<?php
class JSSDK {
  private $appId;
  private $appSecret;

  public function __construct($appId, $appSecret) {
    $this->appId = $appId;
    $this->appSecret = $appSecret;
  }

  public function getSignPackage() {
    $jsapiTicket = getJsApiTicket($this->appId, $this->appSecret);

    // 注意 URL 一定要动态获取，不能 hardcode.
    $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
    $url = "$protocol$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

    $timestamp = time();
    $nonceStr = createNonceStr();

    // 这里参数的顺序要按照 key 值 ASCII 码升序排序
    $string = "jsapi_ticket=$jsapiTicket&noncestr=$nonceStr&timestamp=$timestamp&url=$url";

    $signature = sha1($string);

    $signPackage = array(
      "appId"     => $this->appId,
      "nonceStr"  => $nonceStr,
      "timestamp" => $timestamp,
      "url"       => $url,
      "signature" => $signature,
      "rawString" => $string
    );
    return $signPackage; 
  }
}
//$jssdk = new JSSDK($appid, $secret);
//$signPackage = $jssdk->GetSignPackage();
/*$tmpUrl = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];
if ($_SERVER['QUERY_STRING'] != "") {
	$tmpUrl = $tmpUrl.'?'.$_SERVER['QUERY_STRING'];
}
*/
$tmpUrl = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
//$signPackage = json_decode(file_get_contents($tmpUrl),true);
//echo $signPackage.file_get_contents($tmpUrl);
//$tmp = 'http://test.qixi.360vt.cn/api/getJsapi?url='.'http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING']);
//$res = file_get_contents($url);
//$signPackage = json_decode($res,true);
/*
$url = 'http://test.qixi.360vt.cn/api/getJsapi?url='.'http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING']);*/
$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_TIMEOUT, 500);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_URL, 'http://test.qixi.360vt.cn/api/getJsapi?url='.urlencode($tmpUrl));

$res = curl_exec($curl);
curl_close($curl);
$signPackage = json_decode($res,true);